{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-22T09:23:46.192004Z",
     "start_time": "2025-09-22T09:23:37.544951Z"
    }
   },
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/test2.html')\n",
    "\n",
    "element = wd.find_element(By.CSS_SELECTOR,\n",
    "                          '#s_radio input[name=teacher]:checked')\n",
    "print(element.get_attribute('value'))\n",
    "\n",
    "wd.find_element(By.CSS_SELECTOR, '#s_radio input[value=\"小雷老师\"]').click()\n",
    "print(element.get_attribute('value'))\n",
    "wd.quit()"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小凯老师\n",
      "小凯老师\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-22T09:29:59.589932Z",
     "start_time": "2025-09-22T09:29:48.905154Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/test2.html')\n",
    "\n",
    "element = wd.find_element(By.CSS_SELECTOR, '#s_checkbox input[name=teachers1]:checked')\n",
    "print(element.get_attribute('value'))\n",
    "\n",
    "# 先把 已经选中的选项全部点击一下\n",
    "elements = wd.find_elements(By.CSS_SELECTOR,\n",
    "  '#s_checkbox input[name=\"teachers1\"]:checked')\n",
    "\n",
    "for element in elements:\n",
    "    element.click()\n",
    "\n",
    "# 再点击 小雷老师\n",
    "wd.find_element(By.CSS_SELECTOR,\n",
    "  \"#s_checkbox input[value='小雷老师']\").click()\n",
    "\n",
    "wd.quit()"
   ],
   "id": "96a9ec48caff334c",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小凯老师\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-22T09:46:04.814885Z",
     "start_time": "2025-09-22T09:45:54.299077Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.select import Select\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/test2.html')\n",
    "\n",
    "select = Select(wd.find_element(By.ID, 'ss_single'))\n",
    "# 查看被选中的选项\n",
    "print(select.first_selected_option.get_attribute('value'))\n",
    "\n",
    "select.select_by_value('小雷老师')\n",
    "print(select.first_selected_option.get_attribute('value'))\n",
    "\n",
    "select.select_by_index(1)\n",
    "print(select.first_selected_option.get_attribute('value'))\n",
    "\n",
    "select.select_by_visible_text('小江老师')\n",
    "print(select.first_selected_option.get_attribute('value'))\n",
    "\n",
    "wd.quit()"
   ],
   "id": "f5375f73886984e2",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小凯老师\n",
      "小雷老师\n",
      "小雷老师\n",
      "小江老师\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-22T09:45:15.383087Z",
     "start_time": "2025-09-22T09:45:02.729095Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.select import Select\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/test2.html')\n",
    "\n",
    "# 创建Select对象\n",
    "select = Select(wd.find_element(By.ID, \"ss_multi\"))\n",
    "\n",
    "# 清除所有 已经选中 的选项\n",
    "select.deselect_all()\n",
    "\n",
    "# 选择小雷老师 和 小凯老师\n",
    "select.select_by_visible_text(\"小雷老师\")\n",
    "select.select_by_visible_text(\"小凯老师\")\n",
    "from time import sleep\n",
    "sleep(2)\n",
    "wd.quit()"
   ],
   "id": "b510e72d9d43fbc1",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "1f3ed5d1b764c7d5"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
